/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
 *
 * @author TQNINH
 */
class Student {
    String sCode;
    String sName;
    int mark;
    
    public Student() {
        this.sCode = "";
        this.sName = "";
        this.mark = 0;
    }
    
    public Student(String c, String n, int m) {
        this.sCode = c;
        this.sName = n;
        this.mark = m;
    }
    
    public void saveState() {
        try {
            FileWriter fwriter = new FileWriter("Student.txt", true);
            BufferedWriter writer = new BufferedWriter(fwriter);
            //writer.write(this.sCode + "\t" + this.sName + "\t" + this.mark);
            writer.write(this.sCode);
            writer.write("\t");
            writer.write(this.sName);
            writer.write("\t");
            writer.write(Integer.toString(this.mark));
            writer.write("\t");
            writer.newLine();
            writer.close();
            fwriter.close();
        } catch(FileNotFoundException e) {
            System.out.println("The request file cannot be found.");
        } catch(IOException e) {
            System.out.println("An error occurred while writing to the file.");
        }
    }
    
    public void addNewStudent() {
        Scanner input = new Scanner(System.in);
        System.out.print("Input Student code: ");
        this.sCode = input.nextLine();
        System.out.print("Input Student name: ");
        this.sName = input.nextLine();
        System.out.print("Input Student mark: ");
        this.mark = Integer.parseInt(input.nextLine());
        this.saveState();
    }
    
    public void displayStudent() {
        System.out.println("**** STUDENT DETAIL ****");
        System.out.format("%1$-5s %2$5s %3$5s \n",
                "Code", "Name", "Mark");
        try {
            FileReader input = new FileReader("Student.txt");
            BufferedReader reader = new BufferedReader(input);
            String record;
            while((record = reader.readLine()) != null) {
                StringTokenizer token = new StringTokenizer(record);
                while(token.hasMoreTokens()) {
                    System.out.print(token.nextToken("\t") + "\t\t");
                }
                System.out.println("");
            }
            reader.close();
            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    
    void menu() {
        Scanner inp = new Scanner(System.in);
        int choose = 0;
        boolean flag = false;
        String answer = "";
        do {
            do {
                try {
                    System.out.println("PROGRAM FOR MANAGEMENT OF STUDENT");
                    System.out.println("1. Add new student");
                    System.out.println("2. Dispaly the student");
                    System.out.println("3. Exit");
                    System.out.print("Choose (1-3): ");
                    //choose = inp.nextInt();
                    choose = Integer.parseInt(inp.nextLine());
                    if(choose < 1 || choose > 3) {
                        flag = false;
                        System.out.println("You must accept an integer from 1 to 3! Pls input again");
                    } else {
                        flag = true;
                    }
                } catch (NumberFormatException e) {
                    flag = false;
                    System.out.println("You must accept an integer from 1 to 3! Pls input again");
                }
            } while (!flag);
            switch (choose) {
                case 1:
                    this.addNewStudent();
                    break;
                case 2:
                    this.displayStudent();
                    break;
                case 3:
                    System.out.println("Good bye! See you again!");
                    System.exit(0);
            }
            System.out.print("Do you continue? (Y/N) ");
            answer = inp.nextLine().toUpperCase();                    
        } while(answer.equals("Y"));
    }
}

public class StudentManagement {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Student s = new Student();
        s.menu();
    }
}
